package com.base.pig.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.pig.dto.MonitorAnalysisReportDto;
import com.base.pig.entity.DayReport;
import com.base.pig.entity.MonthReport;
import com.base.pig.entity.ScSensorInfo;
import com.base.pig.service.*;
import com.base.pig.util.DateUtils;
import com.base.pig.util.HttpUtils;
import com.base.pig.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;

/**
 * <p>
 *报表管理
 * </p>
 *
 * @author mag
 * @since 2022-04-08
 */
@RestController
@Api(value = "report", tags = "报表管理")
@RequestMapping("/report")
public class ReportController {

    @Autowired
    public SmregisterService smregisterService;

    @Autowired
    public ScMonitorService monitorService;

    @Autowired
    public ScDeviceService scDeviceService;

    @Autowired
    public ScSensorInfoService scSensorInfoService;

    @Autowired
    public DayReportService dayReportService;


    @Autowired
    public MonthReportService monthReportService;

    @Value("")
    private String sensorLatestData;


    @Value("")
    private String reportData;


    @ApiOperation(value = "定时任务-----------------------------------------------统计昨日日报，月报")
    @PostMapping("/accountDayAndMonthReport")
    public R accountDayAndMonthReport() {



        //查询设备表名
        String deviceTableName= smregisterService.getTableName("DevicePoint");
        //查询全部设备
        QueryWrapper wrapper=new QueryWrapper();
        List<Map<String, Object>> allDeviceList=scDeviceService.queryDeviceInfoByCondition(deviceTableName,wrapper);

        String startTime= DateUtils.getyestoday()+" 00:00:01";
        String  endTime= DateUtils.getyestoday()+" 23:59:59";


        List<DayReport> allDataListDay = new ArrayList<DayReport>();//所有数据

        //2.循环设备，查询
        for (Map<String,Object> deviceMap : allDeviceList) {

            String  deviceId= (String) deviceMap.get("deviceID");


            //2.查询设备下传感器信息
            List<Map<String, Object>> sensorList = scSensorInfoService.querySensorByDevice(deviceTableName, deviceId);


            //3.循环sensor
            sensorList.forEach(sensorinfo -> {


                String sensor_code = (String) sensorinfo.get("sensor_code");
                String sensor_metric = (String) sensorinfo.get("sensor_metric");
                String sensor_unit = (String) sensorinfo.get("sensor_unit");
                String sensor_name = (String) sensorinfo.get("sensor_name");
                String deviceType = (String) sensorinfo.get("DeviceType");
                String DeviceTypeName = (String) sensorinfo.get("DeviceTypeName");
                String pointId = (String) sensorinfo.get("PointID");
                String pointName = (String) sensorinfo.get("PointName");

                if (!StringUtil.isNullOrEmpty(sensor_code)) {//传感器不为null时，调用接口查询数据

                    JSONObject param=new JSONObject();
                    param.put("pointId",pointId);
                    param.put("deviceType",deviceType);
                    param.put("sensorCode",sensor_code);
                    param.put("sensorMetric",sensor_metric);
                    param.put("start",startTime);
                    param.put("end",endTime);
                    param.put("reportType","day");

                    //远程调用
                    System.out.println("地址:"+reportData);
                    System.out.println("参数:"+param);
                    String   result=  HttpUtils.HttpUtils_RequestPost(reportData,param);
                    System.out.println("返回值:"+result);
                    JSONObject jsonObject = JSONObject.parseObject(result);

                    if("200".equals(jsonObject.get("code"))  && "success".equals(jsonObject.get("message"))  ) {


                        JSONObject resultObj = (JSONObject) jsonObject.get("result");

                        JSONArray timePoint = (JSONArray) resultObj.get("timePoint");//时间数组

                        //平均值
                        JSONObject avgVal = (JSONObject) resultObj.get("avgVal");
                        JSONArray dataArray_avg = (JSONArray) avgVal.get("avgDataArray");//
                        //最大值
                        JSONObject maxVal = (JSONObject) resultObj.get("maxVal");
                        JSONArray dataArray_max = (JSONArray) avgVal.get("maxDataArray");//
                        //最小值
                        JSONObject minVal = (JSONObject) resultObj.get("minVal");
                        JSONArray dataArray_min = (JSONArray) avgVal.get("minDataArray");//


                        for (int i = 0; i < timePoint.size(); i++) {

                            DayReport dayReport=new DayReport();
                            dayReport.setDate((String) timePoint.get(i));

                            dayReport.setPointname(pointName);
                            dayReport.setSensorcode(sensor_code);
                            dayReport.setDeviceId(deviceId);

                            if(dataArray_avg.get(i)!=null){
                                dayReport.setAvgvalue((Double) dataArray_avg.get(i));
                            }else{
                                dayReport.setAvgvalue(null);
                            }

                            if(dataArray_max.get(i)!=null){
                                dayReport.setMaxvalue((Double) dataArray_max.get(i));
                            }else{
                                dayReport.setMaxvalue(null);
                            }

                            if(dataArray_min.get(i)!=null){
                                dayReport.setMinvalue((Double) dataArray_min.get(i));
                            }else{
                                dayReport.setMinvalue(null);
                            }
                            allDataListDay.add(dayReport);
                        }
                    }
                }

            });


        }
        //2.批量插入 日报
        dayReportService.saveBatch(allDataListDay);
        //查询数据
        List<MonthReport> allDataListMonth=monthReportService.getGatherMonthReport(DateUtils.getyestoday());

        //3.批量插入 月报
        monthReportService.saveBatch(allDataListMonth);


        return R.ok(null, "查询成功");
    }



    /**
     *年报
     * @param monitorAnalysisDto
     * @return
     */
    @ApiOperation(value = "年报(有bug,还没改)",notes = "参数说明:startMonth(yyyy-mm),endMonth(yyyy-mm),deviceId（多个用,连接）,sensorCode")
    @PostMapping("/getMonitorAnalysisYear")
    public R getMonitorAnalysisYear(@RequestBody MonitorAnalysisReportDto monitorAnalysisDto) throws ParseException {

        String tempDeviceIds =monitorAnalysisDto.getDeviceIds();
        String  sensorCode=monitorAnalysisDto.getSensorCode();
        //校验  deviceId不为空     sensorCode不为空   startTime不为空  endTime不为空  设备类型必须相同
        if(StringUtil.isNullOrEmpty(tempDeviceIds)){
            return R.failed(null,"deviceId不能为空");
        }
        if(StringUtil.isNullOrEmpty(sensorCode)){
            return R.failed(null,"sensorCode不能为空");
        }
        if(StringUtil.isNullOrEmpty(monitorAnalysisDto.getStartMonth())  ||  StringUtil.isNullOrEmpty(monitorAnalysisDto.getStartMonth()) ){
            return R.failed(null,"开始或结束时间不能为空");
        }



        String startTime = monitorAnalysisDto.getStartMonth();
        String endTime = monitorAnalysisDto.getEndMonth();
        String sensorUnit="";
        String sensorName="";
        String[]  deviceIds=tempDeviceIds.split(",");

        //查询设备名称数组
        String deviceTableName= smregisterService.getTableName("DevicePoint");//查询设备表名


        //qq
        QueryWrapper deviceWrapper=new QueryWrapper();
        deviceWrapper.in("deviceId",deviceIds);

        List<Map<String, Object>> dNameList=scDeviceService.queryDeviceInfoByCondition(deviceTableName,deviceWrapper);

        //获取月份信息
        List<String> months=DateUtils.getMonthBetween(startTime,endTime);

        //查询传感器信息
        QueryWrapper<ScSensorInfo> sensorWrapper = new QueryWrapper<ScSensorInfo>();
        sensorWrapper.eq("sensor_code",sensorCode);
        List<ScSensorInfo> scSensorInfoList = scSensorInfoService.list(sensorWrapper);
        if(scSensorInfoList!=null && scSensorInfoList.size()>0 ){
            ScSensorInfo sen = scSensorInfoList.get(0);
            if(sen.getSensorUnit()!=null){
                sensorUnit=sen.getSensorUnit();
                sensorName=sen.getSensorName();
            }
        }else{
            return R.failed(null,"传感器不存在");
        }

        // //1.组装基础数据行
        List<Map<String, Object>> dataRowList=new ArrayList<>();//基础数据行


        for (int i = 0; i <months.size() ; i++) {
            Map<String,Object>  deviceMonirorMap=new HashMap<>();//存储一行监测数据

            Map<String,Object> staMap=new HashMap<>();

            deviceMonirorMap.put("no",i+1);
            deviceMonirorMap.put("date",months.get(i));

            String  month=months.get(i);

            //循环设备
            for (int m=0;m<deviceIds.length;m++) {

                String deviceId = deviceIds[m];

                //查询数据
                List<MonthReport>  monthReportList=monthReportService.getGatherYearReport(month,month,deviceId,sensorCode);


                //普通行
                if(monthReportList!=null  &&  monthReportList.size()>0 ){
                    MonthReport  monthReport=monthReportList.get(0);
                    deviceMonirorMap.put("max-"+deviceId,monthReport.getMax());
                    deviceMonirorMap.put("min-"+deviceId,monthReport.getMin());
                    deviceMonirorMap.put("avg-"+deviceId,monthReport.getAvg());

                }else{
                    deviceMonirorMap.put("max-"+deviceId,null);
                    deviceMonirorMap.put("min-"+deviceId,null);
                    deviceMonirorMap.put("avg-"+deviceId,null);

                }

            }
            dataRowList.add(deviceMonirorMap);
        }

        //3.统计分析行数据
        List<Map<String, Object>> statisticsRow  = getYearStatisticsRow(months,deviceIds,dataRowList,sensorUnit);//得到统计分析的行数据
        //4.标题
        List<Map<String, Object>> titleList=getYearTitle(dNameList,sensorName);//得到标题


        Map<String,Object>  reMap=new HashMap<>();//返回值
        List<Map<String, Object>> rowsList=new ArrayList<>();//存储数据
        //数据合并到一个list中
        rowsList.addAll(dataRowList);
        rowsList.addAll(statisticsRow);

        reMap.put("title",titleList);
        reMap.put("data",rowsList);


        return R.ok(reMap,"查询成功");
    }


    /**
     * 日报
     * @param monitorAnalysisReportDto
     * @return
     */
    @ApiOperation(value = "日报",notes = "参数说明:startDate(yyyy-mm-dd),endDate(yyyy-mm-dd),startTime( hh:mm:ss),endTime( hh:mm:ss)," +
            "deviceIds(,隔开),sensorCode")
    @PostMapping("/getMonitorAnalysisDay")
    public R getMonitorAnalysisDay(@RequestBody MonitorAnalysisReportDto monitorAnalysisReportDto){

        //校验  deviceId不为空     sensorCode不为空   startTime不为空  endTime不为空  interval
        if(StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getDeviceIds())){
            return R.failed(null,"deviceId不能为空");
        }
        if(StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getSensorCode())){
            return R.failed(null,"sensorCode不能为空");
        }
        if(StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getStartTime())
                || StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getEndTime())
                ||   StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getStartDate())
                ||   StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getEndDate()) ){
            return R.failed(null,"开始时间或者结束时间不能为空");
        }
        //if(StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getInterval())
        // || StringUtil.isNullOrEmpty(monitorAnalysisReportDto.getInterval())  ){
        // return R.failed(null,"间隔不能为空");
        // }


        String sensorCode=monitorAnalysisReportDto.getSensorCode();//请求参数
        String startDate = monitorAnalysisReportDto.getStartDate() ;
        String endDate = monitorAnalysisReportDto.getEndDate() ;
        String startTime = monitorAnalysisReportDto.getStartTime();
        String endTime = monitorAnalysisReportDto.getEndTime() ;
        //String interval = monitorAnalysisReportDto.getInterval() ;//间隔
        String interval ="1" ; //间隔暂时默认1小时，后续考虑增加间隔时间条件

        String sensorMetric = "" ;//请求参数
        String sensorUnit = "" ;
        String sensorName= "" ;
        String  startHour=startTime.split(":")[0];
        String  endHour=endTime.split(":")[0];

        //获取最终展示时间列(00:00 01:00 02:00 03:00 04:00)
        List<String>  timeArr=DateUtils.getIntervalHour(startHour,endHour,interval);

        //获取最终展示日期列(2022-01-02  2022-01-03)
        List<String>  dateArr=DateUtils.getBetweenDays(startDate,endDate);

        //1.获取监测数据
        //查询传感器信息
        QueryWrapper<ScSensorInfo> sensorWrapper = new QueryWrapper<ScSensorInfo>();
        sensorWrapper.eq("sensor_code",sensorCode);
        List<ScSensorInfo> scSensorInfoList = scSensorInfoService.list(sensorWrapper);
        if(scSensorInfoList!=null && scSensorInfoList.size()>0 ){
            ScSensorInfo sen = scSensorInfoList.get(0);
            sensorMetric= sen.getSensorMetric();
            if(sen.getSensorUnit()!=null){
                sensorUnit=  sen.getSensorUnit();
            }
            sensorName=sen.getSensorName();
        }else{
            return R.failed(null,"传感器不存在");
        }



        //查询设备信息
        String deviceTableName= smregisterService.getTableName("DevicePoint");

        List<String> deviceIds=new ArrayList<>();
        String[]  deviceIdsArr =monitorAnalysisReportDto.getDeviceIds().split(",");
        for (int o = 0; o <deviceIdsArr.length ; o++) {
            deviceIds.add(deviceIdsArr[o]);
        }

        QueryWrapper deviceWrapper=new QueryWrapper();
        deviceWrapper.in("deviceId",deviceIds);
        List<Map<String, Object>> deviceInfoList=scDeviceService.queryDeviceInfoByCondition(deviceTableName,deviceWrapper);
        if(deviceInfoList==null || deviceInfoList.size()==0 ){
            return R.failed(null,"设备不存在");
        }

        Map<String,Object> deviceMonirorMap=new HashMap<>();//每个设备，每天对应数据


        for (int m = 0; m <deviceInfoList.size() ; m++) {//循环设备

            Map<String, Object>  deviceMap=deviceInfoList.get(m);

            String  deviceId= (String) deviceMap.get("deviceID");
            String  pointId= (String) deviceMap.get("pointID");//参数
            String  deviceType= (String) deviceMap.get("deviceType");//参数

            for (int i = 0; i <dateArr.size() ; i++) {//循环日期

                String startTimeParam = dateArr.get(i) + " " + startHour+":00:00";//请求参数
                String endTimeParam =   dateArr.get(i) + " "  +endHour+ ":59:59";//请求参数
                JSONObject param=new JSONObject();
                param.put("pointId",pointId);
                param.put("deviceType",deviceType);
                param.put("sensorCode",sensorCode);
                param.put("sensorMetric",sensorMetric);
                param.put("start",startTimeParam);
                param.put("end",endTimeParam);
                param.put("reportType","day");

                //远程调用
                System.out.println("地址:"+reportData);
                System.out.println("参数:"+param);
                String   result=  HttpUtils.HttpUtils_RequestPost(reportData,param);
                System.out.println("返回值:"+result);
                //3. 接口返回结果加到最终结果中
                JSONObject jsonObject = JSONObject.parseObject(result);

                if("200".equals(jsonObject.get("code"))  && "success".equals(jsonObject.get("message"))  ){

                    JSONObject resultObj = (JSONObject) jsonObject.get("result");
                    //20220520-继续
                    deviceMonirorMap.put(deviceId+"-"+dateArr.get(i),resultObj);

                }
            }

        }


        //2.组装基础数据行
        List<Map<String, Object>> dataRowList=new ArrayList<>();//基础数据行

        for (int n = 0; n < timeArr.size(); n++) {

            Map<String,Object>  rowMap=new HashMap<>();//存储行数据
            rowMap.put("no",n+1);
            rowMap.put("date",timeArr.get(n));

            //Integer  index=Integer.parseInt(timeArr.get(n).split(":")[0]);
            //System.out.println("日期 ==== " +timeArr.get(n) );
            //循环设备
            for (Map.Entry entry : deviceMonirorMap.entrySet()) {

                String  key= (String) entry.getKey();
                String deviceId=key.split("-")[0];


                JSONObject    resultObj= (JSONObject) entry.getValue();
                JSONArray timePoint = (JSONArray) resultObj.get("timePoint");//时间数组

                //平均值
                JSONObject avgVal = (JSONObject) resultObj.get("avgVal");
                JSONArray dataArray_avg=  (JSONArray) avgVal.get("avgDataArray");//平均值-------平均值值数组
                System.out.println("设备id ==== " +deviceId );

                String  avg=  dataArray_avg.get(n)+"";

                //平均值
                if(avg==null ||  "".equals(avg)  ||  "null".equalsIgnoreCase(avg)  ){
                    rowMap.put(key,"");//deviceID-date
                }else{
                    rowMap.put(key,avg);
                }

            }
            dataRowList.add(rowMap);
        }

        //3.统计分析行数据
        List<Map<String, Object>> statisticsRow  = getDayStatisticsRow(deviceMonirorMap,sensorUnit,timeArr);//得到统计分析的行数据
        //4.标题
        List<Map<String, Object>> titleList=getDayTitle(deviceInfoList,dateArr,sensorName);//得到标题

        Map<String,Object>  reMap=new HashMap<>();//返回值
        List<Map<String, Object>> rowsList=new ArrayList<>();//存储数据
        //数据合并到一个list中
        rowsList.addAll(dataRowList);
        rowsList.addAll(statisticsRow);

        reMap.put("title",titleList);
        reMap.put("data",rowsList);

        return R.ok(reMap,"查询成功");


    }

    /**
     * 月报
     * @param monitorAnalysisDto
     * @return
     */
    @ApiOperation(value = "月报",notes = "参数说明:startTime(yyyy-mm-dd),endTime(yyyy-mm-dd),deviceId（多个用,连接）,sensorCode")
    @PostMapping("/getMonitorAnalysisMonth")
    public R getMonitorAnalysisMonth(@RequestBody MonitorAnalysisReportDto  monitorAnalysisDto){

        String tempDeviceIds =monitorAnalysisDto.getDeviceIds();
        String  sensorCode=monitorAnalysisDto.getSensorCode();//请求参数
        //校验  deviceId不为空     sensorCode不为空   startTime不为空  endTime不为空  设备类型必须相同
        if(StringUtil.isNullOrEmpty(tempDeviceIds)){
            return R.failed(null,"deviceId不能为空");
        }
        if(StringUtil.isNullOrEmpty(sensorCode)){
            return R.failed(null,"sensorCode不能为空");
        }
        if(StringUtil.isNullOrEmpty(monitorAnalysisDto.getStartTime())  ||  StringUtil.isNullOrEmpty(monitorAnalysisDto.getEndTime()) ){
            return R.failed(null,"开始或结束时间不能为空");
        }

        //yyyy-MM-dd
        String startTime = monitorAnalysisDto.getStartTime();//请求参数
        String endTime = monitorAnalysisDto.getEndTime() ;//请求参数


        String  sensorMetric="";//请求参数
        String  sensorUnit="";
        String  sensorName="";
        String deviceTableName= smregisterService.getTableName("DevicePoint");//查询设备表名


        //1.循环设备,暂存监测数据
        String[]  deviceIds=tempDeviceIds.split(",");
        Map<String,Object>  deviceMonirorMap=new HashMap<>();//存储监测数据
        //查询传感器信息
        QueryWrapper<ScSensorInfo> sensorWrapper = new QueryWrapper<ScSensorInfo>();
        sensorWrapper.eq("sensor_code",sensorCode);

        List<ScSensorInfo> scSensorInfoList = scSensorInfoService.list(sensorWrapper);
        if(scSensorInfoList!=null && scSensorInfoList.size()>0 ){
            ScSensorInfo sen = scSensorInfoList.get(0);
            sensorMetric= sen.getSensorMetric();
            if(sen.getSensorUnit()!=null){
                sensorUnit=  sen.getSensorUnit();
            }
            sensorName=sen.getSensorName();
        }else{
            return R.failed(null,"传感器不存在");
        }

        for (int m=0;m<deviceIds.length;m++){

            String deviceId= deviceIds[m];

            String  pointId="";//请求参数
            String  deviceType="";//请求参数
            String  deviceName="";//请求参数
            //查询设设备信息 pointId   deviceType
            QueryWrapper deviceWrapper=new QueryWrapper();
            deviceWrapper.eq("deviceId",deviceId);
            List<Map<String, Object>> list=scDeviceService.queryDeviceInfoByCondition(deviceTableName,deviceWrapper);

            if(list!=null && list.size()>0 ){
                Map<String, Object> map=  list.get(0);
                pointId= (String) map.get("pointID");
                deviceType= (String) map.get("deviceType");
                deviceName= (String) map.get("pointName");
            }else{
                return R.failed(null,"设备不存在");
            }


            //{"sensorMetric":"temperature","pointId":"YSG00001","deviceType":"OutFlow","sensorCode":"0x0502","start":"2022-05-03 16:00:00","end":"2022-05-03 17:00:00","reportType":"hour"}
            JSONObject param=new JSONObject();
            param.put("pointId",pointId);
            param.put("deviceType",deviceType);
            param.put("sensorCode",sensorCode);
            param.put("sensorMetric",sensorMetric);
            param.put("start",startTime);
            param.put("end",endTime);
            param.put("reportType","month");

            //远程调用
            System.out.println("地址:"+reportData);
            System.out.println("参数:"+param);
            String   result=  HttpUtils.HttpUtils_RequestPost(reportData,param);
            System.out.println("返回值:"+result);

            JSONObject jsonObject = JSONObject.parseObject(result);

            if("200".equals(jsonObject.get("code"))  && "success".equals(jsonObject.get("message"))  ){
                JSONObject resultObj = (JSONObject) jsonObject.get("result");
                deviceMonirorMap.put(deviceId+"-"+deviceName,resultObj);
            }
        }

        //2.组装基础数据行
        List<Map<String, Object>> dataRowList=new ArrayList<>();//基础数据行

        List<String>  intervalDays=DateUtils.getBetweenDays(startTime,endTime);

        for (int n = 0; n < intervalDays.size(); n++) {

            Map<String,Object>  rowMap=new HashMap<>();//存储行数据
            rowMap.put("no",n+1);
            rowMap.put("date",intervalDays.get(n));
            System.out.println("日期 ==== " +intervalDays.get(n) );
            //3.循环设备
            for (Map.Entry entry : deviceMonirorMap.entrySet()) {

                String  key= (String) entry.getKey();
                String deviceId=key.split("-")[0];

                JSONObject    resultObj= (JSONObject) entry.getValue();
                JSONArray timePoint = (JSONArray) resultObj.get("timePoint");//时间数组
                //最大值
                JSONObject maxVal = (JSONObject) resultObj.get("maxVal");
                JSONArray dataArray_max=  (JSONArray) maxVal.get("maxDataArray");//最大值-最大值数组
                //最小值
                JSONObject minVal = (JSONObject) resultObj.get("minVal");
                JSONArray dataArray_min=  (JSONArray) minVal.get("minDataArray");//最小值-----最小值数组
                //平均值
                JSONObject avgVal = (JSONObject) resultObj.get("avgVal");
                JSONArray dataArray_avg=  (JSONArray) avgVal.get("avgDataArray");//平均值-------平均值值数组
                System.out.println("设备id ==== " +deviceId );

                String  avg=  dataArray_avg.get(n)+"";
                String  max=  dataArray_max.get(n)+"";
                String  min=  dataArray_min.get(n)+"";

                //平均值
                if(avg==null ||  "".equals(avg)  ||  "null".equalsIgnoreCase(avg)  ){
                    rowMap.put("avgValue"+deviceId,"");
                }else{
                    rowMap.put("avgValue"+deviceId,avg);
                }
                //最大值
                if(max==null ||  "".equals(max)  ||  "null".equalsIgnoreCase(max)  ){
                    rowMap.put("maxValue"+deviceId,"");
                }else{
                    rowMap.put("maxValue"+deviceId,max+sensorUnit);
                }
                //最小值
                if(min==null ||  "".equals(min)  ||  "null".equalsIgnoreCase(min) ){
                    rowMap.put("minValue"+deviceId,"");
                }else{
                    rowMap.put("minValue"+deviceId,min+sensorUnit);
                }
            }
            dataRowList.add(rowMap);
        }

        //3.统计分析行数据
        List<Map<String, Object>> statisticsRow  = getMonthStatisticsRow(deviceMonirorMap,sensorUnit);//得到统计分析的行数据
        //4.标题
        List<Map<String, Object>> titleList=getYearAndMonthTitle(deviceMonirorMap,sensorName);//得到标题

        Map<String,Object>  reMap=new HashMap<>();//返回值
        List<Map<String, Object>> rowsList=new ArrayList<>();//存储数据
        //数据合并到一个list中
        rowsList.addAll(dataRowList);
        rowsList.addAll(statisticsRow);

        reMap.put("title",titleList);
        reMap.put("data",rowsList);

        return R.ok(reMap,"查询成功");
    }


    //获取年报 标题
    public List<Map<String, Object>>    getYearTitle(List<Map<String, Object>> dNameList,String sensorName) {


        List<Map<String, Object>> titleList = new ArrayList<>();

        //序号
        Map<String, Object> noMap = new HashMap<>();
        noMap.put("title", "序号");
        noMap.put("dataIndex", "no");
        noMap.put("key", "no");
        noMap.put("width", "100");
        //时间
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("title", "时间");
        dataMap.put("dataIndex", "date");
        dataMap.put("key", "date");
        dataMap.put("width", "100");

        titleList.add(noMap);
        titleList.add(dataMap);
        //其他
        for (int i = 0; i <dNameList.size() ; i++) {

            Map<String,Object>  map=dNameList.get(i);
            String deviceId= (String) map.get("");
            String deviceName= (String) map.get("");


            Map<String, Object> row1Map = new HashMap<>();//第一行标题
            row1Map.put("title", deviceName);

            List<Map<String, Object>> childrenList_1 = new ArrayList();

            Map<String, Object> row2Map = new HashMap<>();//第二行标题
            row2Map.put("title", sensorName);

            List<Map<String, Object>> childrenList_2 = new ArrayList();

            Map<String, Object> row3Map_avg = new HashMap<>();//第三行标题—
            row3Map_avg.put("title", "平均值");
            row3Map_avg.put("dataIndex", "avg-"+deviceId);
            row3Map_avg.put("key", "avg-"+deviceId);
            row3Map_avg.put("width", "150");

            Map<String, Object> row3Map_max = new HashMap<>();//第三行标题—
            row3Map_max.put("title", "最大值");
            row3Map_max.put("dataIndex", "max-"+deviceId);
            row3Map_max.put("key", "max-"+deviceId);
            row3Map_max.put("width", "150");

            Map<String, Object> row3Map_min = new HashMap<>();//第三行标题—
            row3Map_min.put("title", "最小值");
            row3Map_min.put("dataIndex", "min-"+deviceId);
            row3Map_min.put("key", "min-"+deviceId);
            row3Map_min.put("width", "150");


            childrenList_2.add(row3Map_avg);
            childrenList_2.add(row3Map_max);
            childrenList_2.add(row3Map_min);

            row2Map.put("children", childrenList_2);

            childrenList_1.add(row2Map);

            row1Map.put("children", childrenList_1);

            titleList.add(row1Map);
        }

        return titleList;
    }
    //获取年报统计数据
    public List<Map<String, Object>>    getYearStatisticsRow(List<String>  months,String[] deviceIds ,List<Map<String, Object>> dataRowList,String sensorUnit ) {

        //1。组合数据
        Map<String,Object>  devicesMap=new LinkedHashMap<>();
        for (int i = 0; i < deviceIds.length; i++) {//每个设备存储  最大 最小  平均  三个数组
            String deviceId=deviceIds[i];
            Map<String,Object>  deMap=new HashMap<>();

            List<Object>  avgDeviceList=new ArrayList<>();
            List<Object>  maxDeviceList=new ArrayList<>();
            List<Object>  minDeviceList=new ArrayList<>();


            for (int j = 0; j <months.size(); j++) {// 循环时间

                Map<String,Object>   rowMap=dataRowList.get(j);

                Double avgValue= (Double) rowMap.get("avg-"+deviceId);
                Double maxValue=(Double) rowMap.get("max-"+deviceId);
                Double minValue=(Double) rowMap.get("min-"+deviceId);

                avgDeviceList.add(avgValue);
                maxDeviceList.add(maxValue);
                minDeviceList.add(minValue);
            }

            deMap.put("avg",avgDeviceList);
            deMap.put("max",maxDeviceList);
            deMap.put("min",minDeviceList);

            devicesMap.put(deviceId,deMap);
        }

        //2.获取平均值  最大值  最小值
        List<Map<String, Object>> reList = new ArrayList<>();

        //   最小值  最小值时间    最大值 最大值时间   平均值
        Map<String,Object>  minRowMap=new HashMap<>();
        minRowMap.put("no","");
        minRowMap.put("date","最小值");

        Map<String,Object>  minRowDateMap=new HashMap<>();
        minRowDateMap.put("no","");
        minRowDateMap.put("date","最小值时间");

        Map<String,Object>  maxRowMap=new HashMap<>();
        maxRowMap.put("no","");

        maxRowMap.put("date","最大值");

        Map<String,Object>  maxRowDateMap=new HashMap<>();
        maxRowDateMap.put("no","");
        maxRowDateMap.put("date","最大值时间");

        Map<String,Object>  avgRowMap=new HashMap<>();
        avgRowMap.put("no","");
        avgRowMap.put("date","平均值");


        for (Map.Entry entry : devicesMap.entrySet()) {

            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());

            String  deviceId= (String) entry.getKey();

            Map<String,Object>   map= (Map<String, Object>) entry.getValue();

            //每个设备的 平均值 最大值  最大值时间  最小值    最小值时间


            List<Double>  avgDeviceList= (List<Double>) map.get("avg");
            List<Double>  maxDeviceList= (List<Double>) map.get("max");
            List<Double>  minDeviceList= (List<Double>) map.get("min");



            int maxIndex_avg=getMaxIndex(avgDeviceList);   //平均值 -最大值下标
            int minIndex_avg=getMinIndex(avgDeviceList); //平均值 -最小值下标

            int maxIndex_max=getMaxIndex(maxDeviceList); //最大值 -最大值下标
            int minIndex_max=getMinIndex(maxDeviceList);//最大值 -最小值下标

            int maxIndex_min=getMaxIndex(minDeviceList); //最小值 -最大值下标
            int minIndex_min=getMinIndex(minDeviceList); //最小值 -最小值下标



            //平均值
            Double  avgValue_avg=getAvg(avgDeviceList); //平均值-平均值
            Double  maxValue_avg=avgDeviceList.get(maxIndex_avg); //平均值-最大值
            Double  maxDate_avg=avgDeviceList.get(maxIndex_avg); //平均值-最大值时间
            Double  minValue_avg=avgDeviceList.get(minIndex_avg); //平均值-最小值
            Double  minDate_avg=avgDeviceList.get(minIndex_avg); //平均值-最小值时间

            //最大值
            Double  avgValue_max=getAvg(maxDeviceList); //最大值-平均值
            Double  maxValue_max=maxDeviceList.get(maxIndex_max); //最大值-最大值
            Double  maxDate_max=maxDeviceList.get(maxIndex_max); //最大值-最大值时间
            Double  minValue_max=maxDeviceList.get(minIndex_max); //最大值值-最小值
            Double  minDate_max=maxDeviceList.get(minIndex_max); //最大值-最小值时间

            //最小值
            Double  avgValue_min=getAvg(minDeviceList); //最小值-平均值
            Double  maxValue_min=minDeviceList.get(maxIndex_min); //最大值-最大值
            Double  maxDate_min=minDeviceList.get(maxIndex_min); //最大值-最大值时间
            Double  minValue_min=minDeviceList.get(minIndex_min); //最小值-最小值
            Double  minDate_min=minDeviceList.get(minIndex_min); //最小值-最小值时间

            minRowMap.put("avg-"+deviceId,minValue_avg);
            minRowMap.put("max-"+deviceId,minValue_max);
            minRowMap.put("min-"+deviceId,minValue_min);

            minRowDateMap.put("avg-"+deviceId,minDate_avg);
            minRowDateMap.put("max-"+deviceId,minDate_max);
            minRowDateMap.put("min-"+deviceId,minDate_min);

            maxRowMap.put("avg-"+deviceId,maxValue_avg);
            maxRowMap.put("max-"+deviceId,maxValue_max);
            maxRowMap.put("min-"+deviceId,maxValue_min);

            maxRowDateMap.put("avg-"+deviceId,maxDate_avg);
            maxRowDateMap.put("max-"+deviceId,maxDate_max);
            maxRowDateMap.put("min-"+deviceId,maxDate_min);

            avgRowMap.put("avg-"+deviceId,avgValue_avg);
            avgRowMap.put("max-"+deviceId,avgValue_max);
            avgRowMap.put("min-"+deviceId,avgValue_min);

        }


        reList.add(minRowMap);
        reList.add(minRowDateMap);
        reList.add(maxRowMap);
        reList.add(maxRowDateMap);
        reList.add(avgRowMap);


        return  reList;
    }


    //获取月报统计数据
    public List<Map<String, Object>>    getMonthStatisticsRow(Map<String, Object> deviceMonirorMap,String sensorUnit ) {


        List<Map<String, Object>> reList = new ArrayList<>();


        //   最小值  最小值时间    最大值 最大值时间   平均值
        Map<String,Object>  minRowMap=new HashMap<>();
        minRowMap.put("no","");
        minRowMap.put("date","最小值");

        Map<String,Object>  minRowDateMap=new HashMap<>();
        minRowDateMap.put("no","");
        minRowDateMap.put("date","最小值时间");

        Map<String,Object>  maxRowMap=new HashMap<>();
        maxRowMap.put("no","");
        maxRowMap.put("date","最大值");

        Map<String,Object>  maxRowDateMap=new HashMap<>();
        maxRowDateMap.put("no","");
        maxRowDateMap.put("date","最大值时间");

        Map<String,Object>  avgRowMap=new HashMap<>();
        avgRowMap.put("no","");
        avgRowMap.put("date","平均值");

        for (Map.Entry entry : deviceMonirorMap.entrySet()) {

            String deviceId = (String) entry.getKey();
            JSONObject resultObj = (JSONObject) entry.getValue();

            JSONArray timePoint = (JSONArray) resultObj.get("timePoint");//时间数组

            //平均值
            JSONObject avgVal = (JSONObject) resultObj.get("avgVal");
            //BigDecimal minValData_avg = new BigDecimal(avgVal.get("minValData") + "");//平均值-------最小值
            String minValData_avg = avgVal.get("minValData") + "";//平均值-------最小值
            String minTime_avg = (String) avgVal.get("minTime");//平均值-------最小值时间
            //BigDecimal maxValData_avg = new BigDecimal(avgVal.get("maxValData") + "");//平均值-------最大值
            String maxValData_avg = avgVal.get("maxValData") + "";//平均值-------最大值
            String maxTime_avg = (String) avgVal.get("maxTime");//平均值-------最大值时间
            //BigDecimal avgValData_avg = new BigDecimal(avgVal.get("avgValData") + "");//平均值-------平均值
            String avgValData_avg =avgVal.get("avgValData") + "";//平均值-------平均值

            //最小值
            JSONObject minVal = (JSONObject) resultObj.get("minVal");
            //BigDecimal minValData_min = new BigDecimal(minVal.get("minValData") + "");//最小值-----最小值
            String minValData_min = minVal.get("minValData") + "";//最小值-----最小值
            String minTime_min = (String) minVal.get("minTime");//最小值-----最小值时间
            //BigDecimal maxValData_min = new BigDecimal(minVal.get("maxValData") + "");//最小值-----最大值
            String maxValData_min =minVal.get("maxValData") + "";//最小值-----最大值
            String maxTime_min = (String) minVal.get("maxTime");//最小值-----最大值时间
            //BigDecimal avgValData_min = new BigDecimal(minVal.get("avgValData") + ""); //最小值-----平均值
            String avgValData_min = minVal.get("avgValData") + ""; //最小值-----平均值

            //最大值
            JSONObject maxVal = (JSONObject) resultObj.get("maxVal");
            //BigDecimal minValData_max = new BigDecimal(maxVal.get("minValData") + "");//最大值------最小值
            String minValData_max = maxVal.get("minValData") + "";//最大值------最小值
            String minTime_max = (String) maxVal.get("minTime");//最大值------最小值时间
            //BigDecimal maxValData_max = new BigDecimal(maxVal.get("maxValData") + "");//最大值------最大值
            String maxValData_max = maxVal.get("maxValData") + "";//最大值------最大值
            String maxTime_max = (String) maxVal.get("maxTime");//最大值------最大值时间
            //BigDecimal avgValData_max = new BigDecimal(maxVal.get("avgValData") + "");//最大值------平均值
            String avgValData_max = maxVal.get("avgValData") + "";//最大值------平均值

            //平均值-------最小值
            if (minValData_avg == null || "".equals(minValData_avg)  || "null".equalsIgnoreCase(minValData_avg) ) {
                minRowMap.put("avgValue" + deviceId, "");
            } else {
                minRowMap.put("avgValue" + deviceId, minValData_avg + sensorUnit);
            }
            //最大值------最小值
            if (minValData_max == null || "".equals(minValData_max) || "null".equalsIgnoreCase(minValData_max)) {
                minRowMap.put("maxValue" + deviceId, "");
            } else {
                minRowMap.put("maxValue" + deviceId, minValData_max + sensorUnit);
            }
            //最小值-----最小值
            if (minValData_min == null || "".equals(minValData_min) || "null".equalsIgnoreCase(minValData_min) ) {
                minRowMap.put("minValue" + deviceId, "");
            } else {
                minRowMap.put("minValue" + deviceId, minValData_min + sensorUnit);
            }


            //平均值--------最小值时间
            if (minTime_avg == null || "".equals(minTime_avg)  || "null".equalsIgnoreCase(minTime_avg) ) {
                minRowDateMap.put("avgValue" + deviceId, "");
            } else {
                minRowDateMap.put("avgValue" + deviceId, minTime_avg);
            }
            //最大值--------最小值时间
            if (minTime_max == null || "".equals(minTime_max) || "null".equalsIgnoreCase(minTime_max) ) {
                minRowDateMap.put("maxValue" + deviceId, "");
            } else {
                minRowDateMap.put("maxValue" + deviceId, minTime_max);
            }
            //最小值--------最小值时间
            if (minTime_min == null || "".equals(minTime_min)  || "null".equalsIgnoreCase(minTime_min)) {
                minRowDateMap.put("minValue" + deviceId, "");
            } else {
                minRowDateMap.put("minValue" + deviceId, minTime_min);
            }



            //平均值-------最大值
            if (maxValData_avg == null || "".equals(maxValData_avg)  || "null".equalsIgnoreCase(maxValData_avg) ) {
                maxRowMap.put("avgValue" + deviceId, "");
            } else {
                maxRowMap.put("avgValue" + deviceId, maxValData_avg + sensorUnit);
            }
            //最大值------最大值
            if (maxValData_max == null || "".equals(maxValData_max) || "null".equalsIgnoreCase(maxValData_max)) {
                maxRowMap.put("maxValue" + deviceId, "");
            } else {
                maxRowMap.put("maxValue" + deviceId, maxValData_max + sensorUnit);
            }
            //最小值-----最大值
            if (maxValData_min == null || "".equals(maxValData_min)  || "null".equalsIgnoreCase(maxValData_min) ) {
                maxRowMap.put("minValue" + deviceId, "");
            } else {
                maxRowMap.put("minValue" + deviceId, maxValData_min + sensorUnit);
            }


            //平均值--------最大值时间
            if (maxTime_avg == null || "".equals(maxTime_avg)  || "null".equalsIgnoreCase(maxTime_avg) ) {
                maxRowDateMap.put("avgValue" + deviceId, "");
            } else {
                maxRowDateMap.put("avgValue" + deviceId, maxTime_avg);
            }
            //最大值--------最大值时间
            if (maxTime_max == null || "".equals(maxTime_max)   || "null".equalsIgnoreCase(maxTime_max) ) {
                maxRowDateMap.put("maxValue" + deviceId, "");
            } else {
                maxRowDateMap.put("maxValue" + deviceId, maxTime_max);
            }
            //最小值--------最大值时间
            if (maxTime_min == null || "".equals(maxTime_min)  || "null".equalsIgnoreCase(maxTime_min) ) {
                maxRowDateMap.put("minValue" + deviceId, "");
            } else {
                maxRowDateMap.put("minValue" + deviceId, maxTime_min);
            }


            //平均值-------平均值
            if (avgValData_avg == null || "".equals(avgValData_avg) || "null".equalsIgnoreCase(avgValData_avg) ) {
                avgRowMap.put("avgValue" + deviceId, "");
            } else {
                avgRowMap.put("avgValue" + deviceId, avgValData_avg + sensorUnit);
            }
            //最大值------平均值
            if (avgValData_max == null || "".equals(avgValData_max) ||  "null".equalsIgnoreCase(avgValData_max) ) {
                avgRowMap.put("maxValue" + deviceId, "");
            } else {
                avgRowMap.put("maxValue" + deviceId, avgValData_max + sensorUnit);
            }
            //最小值-----平均值
            if (avgValData_min == null || "".equals(avgValData_min) ||  "null".equalsIgnoreCase(avgValData_min) ) {
                avgRowMap.put("minValue" + deviceId, "");
            } else {
                avgRowMap.put("minValue" + deviceId, avgValData_min + sensorUnit);
            }

        }

        reList.add(minRowMap);
        reList.add(minRowDateMap);
        reList.add(maxRowMap);
        reList.add(maxRowDateMap);
        reList.add(avgRowMap);


        return  reList;
    }

    //获取日报统计数据
    public List<Map<String, Object>>    getDayStatisticsRow(Map<String, Object> deviceMonirorMap,
                                                            String sensorUnit,
                                                            List<String> timeArr) {


        List<Map<String, Object>> reList = new ArrayList<>();

        //   最小值  最小值时间    最大值 最大值时间   平均值
        Map<String,Object>  minRowMap=new HashMap<>();
        minRowMap.put("no","");
        minRowMap.put("date","最小值");

        Map<String,Object>  minRowDateMap=new HashMap<>();
        minRowDateMap.put("no","");
        minRowDateMap.put("date","最小值时间");

        Map<String,Object>  maxRowMap=new HashMap<>();
        maxRowMap.put("no","");
        maxRowMap.put("date","最大值");

        Map<String,Object>  maxRowDateMap=new HashMap<>();
        maxRowDateMap.put("no","");
        maxRowDateMap.put("date","最大值时间");

        Map<String,Object>  avgRowMap=new HashMap<>();
        avgRowMap.put("no","");
        avgRowMap.put("date","平均值");

        for (Map.Entry entry : deviceMonirorMap.entrySet()) {

            String key = (String) entry.getKey();

            //String  deviceId=key.split("-")[0];
            //String  deviceName=key.split("-")[1];


            JSONObject resultObj = (JSONObject) entry.getValue();

            //JSONArray timePoint = (JSONArray) resultObj.get("timePoint");//时间数组

            //平均值
            JSONObject avgVal = (JSONObject) resultObj.get("avgVal");

            String minValData_avg = avgVal.get("minValData") + "";//平均值-------最小值
            String minTime_avg = (String) avgVal.get("minTime");//平均值-------最小值时间
            String maxValData_avg = avgVal.get("maxValData") + "";//平均值-------最大值
            String maxTime_avg = (String) avgVal.get("maxTime");//平均值-------最大值时间
            String avgValData_avg =avgVal.get("avgValData") + "";//平均值-------平均值

            //平均值-------最小值  deviceId+"-"+"deviceName"+"-"+date
            if (minValData_avg == null || "".equals(minValData_avg)  || "null".equalsIgnoreCase(minValData_avg) ) {
                minRowMap.put(key, "");
            } else {
                minRowMap.put(key, minValData_avg + sensorUnit);
            }

            //平均值--------最小值时间
            if (minTime_avg == null || "".equals(minTime_avg)  || "null".equalsIgnoreCase(minTime_avg) ) {
                minRowDateMap.put(key, "");
            } else {
                minRowDateMap.put(key, minTime_avg);
            }


            //平均值-------最大值
            if (maxValData_avg == null || "".equals(maxValData_avg)  || "null".equalsIgnoreCase(maxValData_avg) ) {
                maxRowMap.put(key, "");
            } else {
                maxRowMap.put(key, maxValData_avg + sensorUnit);
            }


            //平均值--------最大值时间
            if (maxTime_avg == null || "".equals(maxTime_avg)  || "null".equalsIgnoreCase(maxTime_avg) ) {
                maxRowDateMap.put(key, "");
            } else {
                maxRowDateMap.put(key, maxTime_avg);
            }


            //平均值-------平均值
            if (avgValData_avg == null || "".equals(avgValData_avg) || "null".equalsIgnoreCase(avgValData_avg) ) {
                avgRowMap.put(key, "");
            } else {
                avgRowMap.put(key, avgValData_avg + sensorUnit);
            }


        }

        reList.add(minRowMap);
        reList.add(minRowDateMap);
        reList.add(maxRowMap);
        reList.add(maxRowDateMap);
        reList.add(avgRowMap);


        return  reList;
    }

    //获取月报/年报 标题
    public List<Map<String, Object>>    getYearAndMonthTitle(Map<String, Object> deviceMonirorMap,String sensorCode) {


        List<Map<String, Object>> titleList = new ArrayList<>();

        //序号
        Map<String, Object> noMap = new HashMap<>();
        noMap.put("title", "序号");
        noMap.put("dataIndex", "no");
        noMap.put("key", "no");
        noMap.put("width", "100");
        //时间
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("title", "时间");
        dataMap.put("dataIndex", "date");
        dataMap.put("key", "date");
        dataMap.put("width", "100");

        titleList.add(noMap);
        titleList.add(dataMap);
        //其他
        for (Map.Entry entry : deviceMonirorMap.entrySet()) {

            String key = (String) entry.getKey();
            String  deviceId=key.split("-")[0];
            String  deviceName=key.split("-")[1];
            JSONObject resultObj = (JSONObject) entry.getValue();

            Map<String, Object> row1Map = new HashMap<>();//第一行标题
            row1Map.put("title", deviceName);

            List<Map<String, Object>> childrenList_1 = new ArrayList();

            Map<String, Object> row2Map = new HashMap<>();//第二行标题
            row2Map.put("title", sensorCode);

            List<Map<String, Object>> childrenList_2 = new ArrayList();

            Map<String, Object> row3Map_avg = new HashMap<>();//第三行标题—
            row3Map_avg.put("title", "平均值");
            row3Map_avg.put("dataIndex", "avgValue"+deviceId);
            row3Map_avg.put("key", "avgValue"+deviceId);
            row3Map_avg.put("width", "150");

            Map<String, Object> row3Map_max = new HashMap<>();//第三行标题—
            row3Map_max.put("title", "最大值");
            row3Map_max.put("dataIndex", "maxValue"+deviceId);
            row3Map_max.put("key", "maxValue"+deviceId);
            row3Map_max.put("width", "150");

            Map<String, Object> row3Map_min = new HashMap<>();//第三行标题—
            row3Map_min.put("title", "最小值");
            row3Map_min.put("dataIndex", "minValue"+deviceId);
            row3Map_min.put("key", "minValue"+deviceId);
            row3Map_min.put("width", "150");


            childrenList_2.add(row3Map_avg);
            childrenList_2.add(row3Map_max);
            childrenList_2.add(row3Map_min);

            row2Map.put("children", childrenList_2);

            childrenList_1.add(row2Map);

            row1Map.put("children", childrenList_1);

            titleList.add(row1Map);
        }

        return titleList;
    }


    //获取日报标题
    public List<Map<String, Object>>    getDayTitle(List<Map<String, Object>> deviceList,
                                                    List<String> dateArr,
                                                    String sensorName) {


        List<Map<String, Object>> titleList = new ArrayList<>();

        //序号
        Map<String, Object> noMap = new HashMap<>();
        noMap.put("title", "序号");
        noMap.put("dataIndex", "no");
        noMap.put("key", "no");
        noMap.put("width", "100");
        //时间
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("title", "时间");
        dataMap.put("dataIndex", "date");
        dataMap.put("key", "date");
        dataMap.put("width", "100");

        titleList.add(noMap);
        titleList.add(dataMap);
        //其他


        for (int i = 0; i < deviceList.size(); i++) {
            // for (Map.Entry entry : deviceMap.entrySet()) {

            //String deviceId = (String) entry.getKey();

            //String deviceName = (String) entry.getValue();

            Map<String, Object> deviceMap=deviceList.get(i);

            String  deviceId= (String) deviceMap.get("deviceID");
            String  deviceName= (String) deviceMap.get("pointName");//参数


            Map<String, Object> row1Map = new HashMap<>();//第一行标题
            row1Map.put("title", deviceName);

            List<Map<String, Object>> childrenList_1 = new ArrayList();

            Map<String, Object> row2Map = new HashMap<>();//第二行标题
            row2Map.put("title", sensorName);

            List<Map<String, Object>> childrenList_2 = new ArrayList();


            for (int j = 0; j < dateArr.size(); j++) {

                Map<String, Object> row3Map = new HashMap<>();//第三行标题—
                row3Map.put("title", dateArr.get(j));
                row3Map.put("dataIndex", deviceId+"-"+dateArr.get(j));
                row3Map.put("key", deviceId+"-"+dateArr.get(j));
                row3Map.put("width", "150");
                //deviceId+"-"+"deviceName"+"-"+dateArr.get(i)
                childrenList_2.add(row3Map);

            }


            row2Map.put("children", childrenList_2);

            childrenList_1.add(row2Map);

            row1Map.put("children", childrenList_1);

            titleList.add(row1Map);
        }

        return titleList;
    }



    public  int getMaxIndex(List<Double> a){

        int  re=0;

        //声明2个变量，取数组任意两个值进行赋值
        Double max=null;
        for (int i = 0; i < a.size(); i++) {//取出第一个不为null的作为初始比较值
            if(a.get(i)!=null){
                max=a.get(i);
            }
        }
        if(max==null){//全为null,返回第一个
            return re;
        }

         //遍历数组，分别与这两值比较，比当前最大值大时，把它赋值给最大值，比当前最小值小时，把它赋值给最小值。
        for (int i = 0; i < a.size(); i++) {
            if(a.get(i)==null){//如果是null,下次循环
                continue;
            }
            if (a.get(i) >=max) {
                re=i;
                max=a.get(i);
            }
        }
       return re;
    }

    public  int getMinIndex(List<Double> a){
        int  re=0;
        Double min=null;
        for (int i = 0; i < a.size(); i++) {//取出第一个不为null的作为初始比较值
            if(a.get(i)!=null){
                min=a.get(i);
            }
        }
        if(min==null){//全为null,返回第一个
            return re;
        }

        //遍历数组，分别与这两值比较，比当前最大值大时，把它赋值给最大值，比当前最小值小时，把它赋值给最小值。
        for (int i = 0; i < a.size(); i++) {
            if(a.get(i)==null){//如果是null,下次循环
                continue;
            }
            if(a.get(i) <=min){
                re=i;
                min=a.get(i);
            }
        }
        return re;
    }

    public Double getAvg(List<Double> a){
        //声明2个变量，取数组任意两个值进行赋值
        //List<Double> list = new ArrayList<>();
        Double sum = 0.0;
        double avg=0.0;
        int num=0;
        for (Double i : a) {
            if(i==null){//如果是null,下次循环
                continue;
            }
            sum += i;
            num++;
        }
        if(a != null && a.size() > 0 ){
            avg=sum / num;
        }
        return  avg;
    }

}
